/*
 Copyright (c) 2012 Seth Juarez

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

using System;
using numl.Data;
using numl.Utils;
using numl.Model;
using System.Data;
using System.Linq;
using numl.Tests.Data;
using NUnit.Framework;
using System.Collections.Generic;

namespace numl.Tests.DataTests
{
    [TestFixture]
    public class SimpleConversionTests
    {
        public static IEnumerable<string> ShortStrings
        {
            get
            {
                yield return "OnE!";
                yield return "TWo2";
                yield return "t#hrE^e";
                yield return "T%hrE@e";
            }
        }

        public static IEnumerable<string> WordStrings
        {
            get
            {
                yield return "TH@e QUi#CK bRow!N fox 12";
                yield return "s@upeR B#roWN BEaR";
                yield return "1829! UGlY FoX";
                yield return "ThE QuICk beAr";
            }
        }

        [Test]
        public void Test_Univariate_Conversions()
        {
            // boolean
            Assert.AreEqual(1, FastReflection.Convert(true));
            Assert.AreEqual(-1, FastReflection.Convert(false));

            // numeric types
            Assert.AreEqual(1d, FastReflection.Convert((Byte)1));                            // byte
            Assert.AreEqual(1d, FastReflection.Convert((SByte)1));                           // sbyte
            Assert.AreEqual(0.4d, FastReflection.Convert((Decimal)0.4m));                    // decimal
            Assert.AreEqual(0.1d, FastReflection.Convert((Double)0.1d));                     // double
            Assert.AreEqual(300d, FastReflection.Convert((Single)300f));                     // single
            Assert.AreEqual(1d, FastReflection.Convert((Int16)1));                           // int16
            Assert.AreEqual(2d, FastReflection.Convert((UInt16)2));                          // uint16
            Assert.AreEqual(2323432, FastReflection.Convert((Int32)2323432));                // int32
            Assert.AreEqual(2323432d, FastReflection.Convert((UInt32)2323432));              // uint32
            Assert.AreEqual(1232323434345d, FastReflection.Convert((Int64)1232323434345));   // int64
            Assert.AreEqual(1232323434345d, FastReflection.Convert((UInt64)1232323434345));  // uint64
            Assert.AreEqual(1232323434345d, FastReflection.Convert((long)1232323434345));    // long

            // enum
            Assert.AreEqual(0d, FastReflection.Convert(FakeEnum.Item0));
            Assert.AreEqual(1d, FastReflection.Convert(FakeEnum.Item1));
            Assert.AreEqual(2d, FastReflection.Convert(FakeEnum.Item2));
            Assert.AreEqual(3d, FastReflection.Convert(FakeEnum.Item3));
            Assert.AreEqual(4d, FastReflection.Convert(FakeEnum.Item4));
            Assert.AreEqual(5d, FastReflection.Convert(FakeEnum.Item5));
            Assert.AreEqual(6d, FastReflection.Convert(FakeEnum.Item6));
            Assert.AreEqual(7d, FastReflection.Convert(FakeEnum.Item7));
            Assert.AreEqual(8d, FastReflection.Convert(FakeEnum.Item8));
            Assert.AreEqual(9d, FastReflection.Convert(FakeEnum.Item9));

            // char
            Assert.AreEqual(65d, FastReflection.Convert('A'));

            // timespan
            Assert.AreEqual(300d, FastReflection.Convert(TimeSpan.FromSeconds(300)));
        }

        [Test]
        public void Test_Char_Dictionary_Gen()
        {
            var d = StringHelpers.BuildCharDictionary(ShortStrings);
            // should have the following:
            // O(2), N(1), E(5), #SYM#(5), T(3), W(1), #NUM#(1), H(2), R(2)
            var dict = new Dictionary<string, double>
            {
                { "O", 2 },
                { "N", 1 },
                { "E", 5 },
                { "#SYM#", 5 },
                { "T", 3 },
                { "W", 1 },
                { "#NUM#", 1 },
                { "H", 2 },
                { "R", 2 },
            };

            Assert.AreEqual(dict, d);
        }

        [Test]
        public void Test_Enum_Dictionary_Gen()
        {
            var d = StringHelpers.BuildEnumDictionary(ShortStrings);
            // should have the following:
            // ONE(1), TWO2(1), THREE(2)
            var dict = new Dictionary<string, double>
            {
                { "ONE", 1 },
                { "TWO2", 1 },
                { "THREE", 2 },
            };

            Assert.AreEqual(dict, d);
        }

        [Test]
        public void Test_Word_Dictionary_Gen()
        {
            var d = StringHelpers.BuildWordDictionary(WordStrings);
            // should have the following:
            var dict = new Dictionary<string, double>
            {
                { "THE", 2 },
                { "QUICK", 2 },
                { "BROWN", 2 },
                { "FOX", 2 },
                { "#NUM#", 2 },
                { "SUPER", 1 },
                { "BEAR", 2 },
                { "UGLY", 1 },
            };

            Assert.AreEqual(dict, d);
        }

        [Test]
        public void Test_Fast_Reflection_Get_Standard()
        {
            var o = new Student
            {
                Age = 23,
                Friends = 12,
                GPA = 3.2,
                Grade = Grade.A,
                Name = "Jordan Spears",
                Tall = true,
                Nice = false
            };

            var age = FastReflection.Get(o, "Age");
            Assert.AreEqual(23, (int)age);
            var friends = FastReflection.Get(o, "Friends");
            Assert.AreEqual(12, (int)friends);
            var gpa = FastReflection.Get(o, "GPA");
            Assert.AreEqual(3.2, (double)gpa);
            var grade = FastReflection.Get(o, "Grade");
            Assert.AreEqual(Grade.A, (Grade)grade);
            var name = FastReflection.Get(o, "Name");
            Assert.AreEqual("Jordan Spears", (string)name);
            var tall = FastReflection.Get(o, "Tall");
            Assert.AreEqual(true, (bool)tall);
            var nice = FastReflection.Get(o, "Nice");
            Assert.AreEqual(false, (bool)nice);
        }

        [Test]
        public void Test_Fast_Reflection_Set_Standard()
        {
            var o = new Student
            {
                Age = 23,
                Friends = 12,
                GPA = 3.2,
                Grade = Grade.A,
                Name = "Jordan Spears",
                Tall = true,
                Nice = false
            };

            FastReflection.Set(o, "Age", 25);
            Assert.AreEqual(25, o.Age);

            FastReflection.Set(o, "Friends", 1);
            Assert.AreEqual(1, o.Friends);

            FastReflection.Set(o, "GPA", 1.2);
            Assert.AreEqual(1.2, o.GPA);

            FastReflection.Set(o, "Grade", Grade.C);
            Assert.AreEqual(Grade.C, o.Grade);

            FastReflection.Set(o, "Name", "Seth Juarez");
            Assert.AreEqual("Seth Juarez", o.Name);

            FastReflection.Set(o, "Tall", false);
            Assert.AreEqual(false, o.Tall);

            FastReflection.Set(o, "Nice", true);
            Assert.AreEqual(true, o.Nice);
        }

        [Test]
        public void Test_Fast_Reflection_Get_Dictionary()
        {
            var o = new Dictionary<string, object>();
            o["Age"] = 23;
            o["Friends"] = 12;
            o["GPA"] = 3.2;
            o["Grade"] = Grade.A;
            o["Name"] = "Jordan Spears";
            o["Tall"] = true;
            o["Nice"] = false;

            var age = FastReflection.Get(o, "Age");
            Assert.AreEqual(23, (int)age);
            var friends = FastReflection.Get(o, "Friends");
            Assert.AreEqual(12, (int)friends);
            var gpa = FastReflection.Get(o, "GPA");
            Assert.AreEqual(3.2, (double)gpa);
            var grade = FastReflection.Get(o, "Grade");
            Assert.AreEqual(Grade.A, (Grade)grade);
            var name = FastReflection.Get(o, "Name");
            Assert.AreEqual("Jordan Spears", (string)name);
            var tall = FastReflection.Get(o, "Tall");
            Assert.AreEqual(true, (bool)tall);
            var nice = FastReflection.Get(o, "Nice");
            Assert.AreEqual(false, (bool)nice);
        }

        [Test]
        public void Test_Fast_Reflection_Set_Dictionary()
        {
            var o = new Dictionary<string, object>();
            o["Age"] = 23;
            o["Friends"] = 12;
            o["GPA"] = 3.2;
            o["Grade"] = Grade.A;
            o["Name"] = "Jordan Spears";
            o["Tall"] = true;
            o["Nice"] = false;

            FastReflection.Set(o, "Age", 25);
            Assert.AreEqual(25, o["Age"]);

            FastReflection.Set(o, "Friends", 1);
            Assert.AreEqual(1, o["Friends"]);

            FastReflection.Set(o, "GPA", 1.2);
            Assert.AreEqual(1.2, o["GPA"]);

            FastReflection.Set(o, "Grade", Grade.C);
            Assert.AreEqual(Grade.C, o["Grade"]);

            FastReflection.Set(o, "Name", "Seth Juarez");
            Assert.AreEqual("Seth Juarez", o["Name"]);

            FastReflection.Set(o, "Tall", false);
            Assert.AreEqual(false, o["Tall"]);

            FastReflection.Set(o, "Nice", true);
            Assert.AreEqual(true, o["Nice"]);
        }

        [Test]
        public void Test_Vector_Conversion_Simple_Numbers()
        {
            Descriptor d = new Descriptor();
            d.Features = new Property[]
            {
                new Property { Name = "Age", Type = typeof(int) },
                new Property { Name = "Height", Type=typeof(double) },
                new Property { Name = "Weight", Type = typeof(decimal) },
                new Property { Name = "Good", Type=typeof(bool) },
            };

            var o = new { Age = 23, Height = 6.21d, Weight = 220m, Good = false };

            var truths = new double[] { 23, 6.21, 220, -1 };
            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }

        [Test]
        public void Test_Vector_DataRow_Conversion_Simple_Numbers()
        {
            Descriptor d = new Descriptor();
            d.Features = new Property[]
            {
                new Property { Name = "Age", },
                new Property { Name = "Height", },
                new Property { Name = "Weight", },
                new Property { Name = "Good", },
            };

            DataTable table = new DataTable("student");
            var age = table.Columns.Add("Age", typeof(int));
            var height = table.Columns.Add("Height", typeof(double));
            var weight = table.Columns.Add("Weight", typeof(decimal));
            var good = table.Columns.Add("Good", typeof(bool));

            DataRow row = table.NewRow();
            row[age] = 23;
            row[height] = 6.21;
            row[weight] = 220m;
            row[good] = false;

            var truths = new double[] { 23, 6.21, 220, -1 };
            var actual = d.Convert(row);
            Assert.AreEqual(truths, actual);
        }

        [Test]
        public void Test_Vector_Dictionary_Conversion_Simple_Numbers()
        {
            Descriptor d = new Descriptor();
            d.Features = new Property[]
            {
                new Property { Name = "Age", },
                new Property { Name = "Height", },
                new Property { Name = "Weight", },
                new Property { Name = "Good", },
            };

            Dictionary<string, object> o = new Dictionary<string, object>();
            o["Age"] = 23;
            o["Height"] = 6.21;
            o["Weight"] = 220m;
            o["Good"] = false;

            var truths = new double[] { 23, 6.21, 220, -1 };
            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }

        [Test]
        public void Test_Vector_Conversion_Simple_Numbers_And_Strings()
        {
            Descriptor d = new Descriptor();

            var dictionary = StringHelpers.BuildWordDictionary(WordStrings)
                                          .Select(k => k.Key)
                                          .ToArray();

            d.Features = new Property[]
            {
                new Property { Name = "Age" },
                new Property { Name = "Height" },
                new StringProperty { Name = "Words", Dictionary = dictionary },
                new Property { Name = "Weight" },
                new Property { Name = "Good" },
                
            };

            // [THE, QUICK, BROWN, FOX, #NUM#, SUPER, BEAR, UGLY]
            // [  1,     0,     1,   0,     1,     0,    0,    0]

            var o = new { Age = 23, Height = 6.21d, Weight = 220m, Good = false, Words = "the brown 432" };

            // array generated by descriptor ordering
            var truths = new double[] { 23, 6.21,  
                                        /* BEGIN TEXT */ 
                                        1, 0, 1, 0, 1, 0, 0, 0,
                                        /* END TEXT */
                                        220, -1 };
            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
            // offset test
            Assert.AreEqual(10, d.Features[3].Start);
            Assert.AreEqual(11, d.Features[4].Start);
        }

        [Test]
        public void Test_Vector_Conversion_Simple_Numbers_And_Strings_As_Enum()
        {
            Descriptor d = new Descriptor();

            var dictionary = StringHelpers.BuildWordDictionary(WordStrings)
                                          .Select(k => k.Key)
                                          .ToArray();

            d.Features = new Property[]
            {
                new Property { Name = "Age" },
                new Property { Name = "Height" },
                new StringProperty { Name = "Words", Dictionary = dictionary, AsEnum = true },
                new Property { Name = "Weight" },
                new Property { Name = "Good" },
                
            };

            // [THE, QUICK, BROWN, FOX, #NUM#, SUPER, BEAR, UGLY]
            // [  0,     1,     2,   3,     4,     5,    6,    7]

            var o = new { Age = 23, Height = 6.21d, Weight = 220m, Good = false, Words = "432342" };

            // array generated by descriptor ordering
            var truths = new double[] { 23, 6.21,  
                                        /* BEGIN TEXT */ 
                                        4,
                                        /* END TEXT */
                                        220, -1 };
            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);


            o = new { Age = 23, Height = 6.21d, Weight = 220m, Good = false, Words = "sUpEr" };
            // array generated by descriptor ordering
            truths = new double[] { 23, 6.21,  
                                        /* BEGIN TEXT */ 
                                        5,
                                        /* END TEXT */
                                        220, -1 };
            actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }

        [Test]
        public void Test_Vector_Conversion_Simple_Numbers_And_Chars()
        {
            Descriptor d = new Descriptor();

            var dictionary = StringHelpers.BuildCharDictionary(ShortStrings)
                                          .Select(k => k.Key)
                                          .ToArray();

            d.Features = new Property[]
            {
                new Property { Name = "Age" },
                new Property { Name = "Height" },
                new StringProperty { 
                    Name = "Chars", 
                    Dictionary = dictionary, 
                    SplitType = StringSplitType.Character 
                },
                new Property { Name = "Weight" },
                new Property { Name = "Good" },
                
            };

            // ["O",  "N",  "E", "#SYM#", "T", "W", "#NUM#", "H", "R"]
            // [  2,    1,   0,        1,   0,   0,       1,   0,   3]

            var o = new { Age = 23, Height = 6.21d, Chars = "oon!3RrR", Weight = 220m, Good = false, };

            // array generated by descriptor ordering
            var truths = new double[] { 23, 6.21,  
                                        /* BEGIN CHARS */ 
                                        2, 1, 0, 1, 0, 0, 1, 0, 3,
                                        /* END CHARS */
                                        220, -1 };
            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }

        [Test]
        public void Test_Vector_Conversion_Simple_Numbers_And_Chars_As_Enum()
        {
            Descriptor d = new Descriptor();

            var dictionary = StringHelpers.BuildCharDictionary(ShortStrings)
                                          .Select(k => k.Key)
                                          .ToArray();

            d.Features = new Property[]
            {
                new Property { Name = "Age" },
                new Property { Name = "Height" },
                new StringProperty { 
                    Name = "Chars", 
                    Dictionary = dictionary, 
                    SplitType = StringSplitType.Character,
                    AsEnum = true
                },
                new Property { Name = "Weight" },
                new Property { Name = "Good" },
                
            };

            // ["O",  "N",  "E", "#SYM#", "T", "W", "#NUM#", "H", "R"]
            // [  2,    1,   0,        1,   0,   0,       1,   0,   3]

            var o = new { Age = 23, Height = 6.21d, Chars = "333", Weight = 220m, Good = false, };

            // array generated by descriptor ordering
            var truths = new double[] { 23, 6.21,  
                                        /* BEGIN CHARS */ 
                                        6,
                                        /* END CHARS */
                                        220, -1 };

            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);


            o = new { Age = 23, Height = 6.21d, Chars = "!", Weight = 220m, Good = false, };

            // array generated by descriptor ordering
            truths = new double[] { 23, 6.21,  
                                    /* BEGIN CHARS */ 
                                    3,
                                    /* END CHARS */
                                    220, -1 };

            actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }

        [Test]
        public void Test_Matrix_Conversion_Simple()
        {
            Descriptor d = new Descriptor();

            d.Features = new Property[]
            {
                new Property { Name = "Age" },
                new Property { Name = "Height" },
                new Property { Name = "Weight" },
                new Property { Name = "Good" },
            };

            var o = new[] {
                new { Age = 23, Height = 6.21d, Weight = 220m, Good = false },
                new { Age = 12, Height = 4.2d, Weight = 120m, Good = true },
                new { Age = 9, Height = 6.0d, Weight = 340m, Good = true },
                new { Age = 87, Height = 3.7d, Weight = 79m, Good = false }
            };

            // array generated by descriptor ordering
            // (returns IEnumerable, but should pass)
            var truths = new double[][] {
                new double[] { 23, 6.21, 220, -1 },
                new double[] { 12,  4.2,  120, 1 },
                new double[] { 9,  6.0,  340,  1 },
                new double[] { 87,  3.7,  79,  -1 }
            };

            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }


        [Test]
        public void Test_Matrix_Conversion_With_Label()
        {
            Descriptor d = new Descriptor();

            d.Features = new Property[]
            {
                new Property { Name = "Age" },
                new Property { Name = "Height" },
                new Property { Name = "Weight" },
                new Property { Name = "Good" },
            };

            d.Label = new Property { Name = "Nice" };

            var o = new[] {
                new { Age = 23, Height = 6.21d, Weight = 220m, Good = false, Nice = true },
                new { Age = 12, Height = 4.2d, Weight = 120m, Good = true, Nice = false },
                new { Age = 9, Height = 6.0d, Weight = 340m, Good = true, Nice = true },
                new { Age = 87, Height = 3.7d, Weight = 79m, Good = false, Nice = false }
            };

            // array generated by descriptor ordering
            // (returns IEnumerable, but should pass)
            var truths = new double[][] {
                new double[] { 23, 6.21, 220, -1, 1 },
                new double[] { 12,  4.2,  120, 1, -1 },
                new double[] { 9,  6.0,  340,  1, 1 },
                new double[] { 87,  3.7,  79,  -1, -1 }
            };

            var actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }
    }
}
